package com.example.its2021606134chh.LoginModule;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.its2021606134chh.MainActivity;
import com.example.its2021606134chh.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {

    private TextView registerTitle;
    private TextView tvNetWork2;
    private EditText edtRegUserName;
    private EditText edtRegPwd;
    private EditText edtRegAgaPwd;
    private EditText edtRegPhone;
    private Button btnRegCancel;
    private Button btnRegRegister;
    private String user;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        user = edtRegUserName.getText().toString();
        password = edtRegPwd.getText().toString();
        Log.e("user", user);
        Log.e("password", password);
        String aga_password = edtRegAgaPwd.getText().toString();
        String phone = edtRegPhone.getText().toString();

        btnRegCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        btnRegRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //        手机号格式正则表达式
                String phone = edtRegPhone.getText().toString();
                String regex = "^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";
                boolean flag = phone.matches(regex);

                //        手机号位数检查
                if (edtRegPhone.length() != 11){
                    Toast.makeText(RegisterActivity.this,"手机号位数不够",Toast.LENGTH_SHORT).show();
                }
                //        手机号格式检查
                else if (!flag) {
                    Toast.makeText(RegisterActivity.this, "手机号格式出错", Toast.LENGTH_SHORT).show();
                }
                //        两次密码检查
                else if (!password.equals(aga_password)) {
                    Toast.makeText(RegisterActivity.this, "2次输入密码不同", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
        tvNetWork2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void initView() {
        registerTitle = (TextView) findViewById(R.id.registerTitle);
        tvNetWork2 = (TextView) findViewById(R.id.tvNetWork2);
        edtRegUserName = (EditText) findViewById(R.id.edtRegUserName);
        edtRegPwd = (EditText) findViewById(R.id.edtRegPwd);
        edtRegAgaPwd = (EditText) findViewById(R.id.edtRegAgaPwd);
        edtRegPhone = (EditText) findViewById(R.id.edtRegPhone);
        btnRegCancel = (Button) findViewById(R.id.btnRegCancel);
        btnRegRegister = (Button) findViewById(R.id.btnRegRegister);
    }
}